﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Tools
{
    public partial class DOMSourceForm : InnerFormBase
    {
        public DOMSourceForm()
        {
            InitializeComponent();
        }

        private IHTMLFilter _filter;
        public IHTMLFilter Filter
        {
            get
            {
                return _filter;
            }
            set
            {
                _filter = value;
                trvDOM.Nodes.Clear();
                List<FilterResult> results = _filter.GetInputHTML();
                if (results == null) return;
                foreach (var result in results)
                {
                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(result.HTMLResult);
                    TreeNode root=trvDOM.Nodes.Add ("Root");
                    root.Tag = doc.DocumentNode;
                    AddNode(root, doc.DocumentNode);
                }

            }
        }

        private void AddNode(TreeNode treeNode,HtmlAgilityPack.HtmlNode htmlNode)
        {
            foreach (HtmlAgilityPack.HtmlNode node in htmlNode.ChildNodes)
            {
                if (node.Name.StartsWith("#")) continue;
                StringBuilder sbAttr = new StringBuilder();
                foreach (var attr in node.Attributes)
                {
                    sbAttr.AppendFormat(" {0}='{1}'", attr.Name, attr.Value);
                }
                string nodeName = string.Format("<{0}{1}>", node.Name, sbAttr.ToString());
                TreeNode tNode = treeNode.Nodes.Add(nodeName);
                tNode.Tag = node;

                if (node.ChildNodes.Count == 1 && node.ChildNodes[0].Name.StartsWith("#"))
                {
                    tNode.Nodes.Add(node.ChildNodes[0].InnerText).Tag = node.ChildNodes[0];
                }
                else
                {
                    tNode.Nodes.Add("");
                }
                //AddNode(tNode, node);
            }
        }

        private void trvDOM_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Tag == null)
            {
                e.Node.Nodes.Clear();
            }
            else
            {
                return;
            }

            HtmlAgilityPack.HtmlNode htmlNode = e.Node.Tag as HtmlAgilityPack.HtmlNode;
            if (htmlNode != null)
            {
                AddNode(e.Node, htmlNode);
            }
        }

        private void trvDOM_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtXPath.Clear();
            HtmlAgilityPack.HtmlNode htmlNode = e.Node.Tag as HtmlAgilityPack.HtmlNode;
            if (htmlNode != null)
            {
                txtXPath.AppendText(htmlNode.XPath);
                txtXPath.AppendText(Environment.NewLine);
                txtXPath.AppendText(Environment.NewLine);
                txtXPath.AppendText(htmlNode.InnerText);
            }
        }

    }
}
